#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
from collections import OrderedDict
import logging

__author__ = 'lizige'
logger = logging.getLogger(__name__)


def init(app):
    app.jinja_env.filters['ifNoneParseNullString'] = ifNoneParseNullString
    app.jinja_env.filters['formatDate'] = formatDate
    app.jinja_env.filters['splitStr'] = splitStr
    app.jinja_env.filters['isDict'] = isDict
    app.jinja_env.filters['isList'] = isList
    app.jinja_env.filters['parseArray'] = parseArray


def ifNoneParseNullString(data):
    if data:
        return data
    else:
        return ""


def formatDate(str, formatStr=None):
    if str:
        t1 = datetime.now()
        if len(str) == 14:
            t1 = datetime.strptime(str, "%Y%m%d%H%M%S")
        elif len(str) == 10:
            t1 = datetime.strptime(str, "%Y-%m-%d")
        if not formatStr:
            formatStr = '%Y-%m-%d'
        return t1.strftime(formatStr)
    return ""


def splitStr(data, dot=":"):
    if data:
        if "".find(dot):
            return data.split(dot)
        else:
            return data
    else:
        return ""


def isDict(data):
    if data:
        if type(data) == OrderedDict or type(data) == dict:
            return True
    logger.warning(data)
    return False


def isList(data):
    if data:
        if type(data) == list:
            return True
    logger.warning(data)
    return False


def parseArray(data):
    arr = []
    if data:
        arr.append(data)
    return arr


if __name__ == "__main__":
    print(formatDate("20191029"))
